<template>
    <div class="schedule-schedule-management">
        <el-card class="box-card">
            <!-- 排班模板编辑对话框 -->
            <el-dialog
                    :title="modelType === 0 ? '新增排班模板' : '编辑排班模板'"
                    :visible.sync="dialogVisible"
                    width="50%"
                    :before-close="handleClose">
                <el-form
                        ref="form"
                        :rules="rules"
                        :model="form"
                        label-width="120px"
                        class="demo-form-inline">
                    <el-form-item label="模板名称" prop="name">
                        <el-input v-model="form.name" clearable placeholder="模板名称"></el-input>
                    </el-form-item>
                    <el-form-item label="模板描述" prop="description">
                        <el-input
                                type="textarea"
                                v-model="form.description"
                                clearable
                                placeholder="模板描述"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
            </el-dialog>

            <!-- 排班模板列表 -->
            <div class="manage-header">
                <el-button
                        style="margin: 0 0 10px 0"
                        size="small"
                        type="primary"
                        @click="handleAdd">新增排班模板</el-button>
            </div>
            <div class="common-table">
                <el-table
                        :data="schedules"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            show-overflow-tooltip
                            prop="name"
                            label="模板名称"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            show-overflow-tooltip
                            prop="description"
                            label="模板描述"
                            width="200">
                    </el-table-column>
                    <el-table-column
                            fixed="right"
                            width="500"
                            label="操作">
                        <schedule slot-scope="scope">
                            <el-button
                                    type="primary"
                                    plain
                                    size="mini"
                                    @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button
                                    plain
                                    size="mini"
                                    type="danger"
                                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                            <el-button
                                    type="success"
                                    plain
                                    size="mini"
                                    @click="handleViewDetails(scope.row)">查看排班详情</el-button>
                        </schedule>
                    </el-table-column>
                </el-table>
            </div>
        </el-card>
    </div>
</template>

<script>
    import {getScheduleList, addSchedule, updateSchedule, deleteScheduleById} from "@/api/Schedule";

    export default {
        data() {
            return {
                dialogVisible: false,
                form: {
                    name: "",
                    description: ""
                },
                rules: {
                    name: [{ required: true, message: "请输入模板名称", trigger: "blur" }]
                },
                schedules: [],
                modelType: 0
            };
        },
        methods: {
            submit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.modelType === 0) {
                            addSchedule(this.form).then((res) => {
                                if (res.data.code === 200) {
                                    this.$message.success("添加成功");
                                    this.getList();
                                } else {
                                    this.$message.error(res.data.message);
                                }
                                this.dialogVisible = false;
                            });
                        } else {
                            updateSchedule(this.form).then((res) => {
                                if (res.data.code === 200) {
                                    this.$message.success("修改成功");
                                    this.getList();
                                } else {
                                    this.$message.error(res.data.message);
                                }
                                this.dialogVisible = false;
                            });
                        }
                    }
                });
            },
            handleClose() {
                this.form = {};
                this.dialogVisible = false;
            },
            cancel() {
                this.handleClose();
            },
            handleAdd() {
                this.modelType = 0;
                this.dialogVisible = true;
            },
            handleEdit(index, row) {
                this.modelType = 1;
                this.dialogVisible = true;
                this.form = { ...row };
            },
            handleDelete(index, row) {
                this.$confirm("此操作将永久删除该模板, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        deleteScheduleById(row.id).then((res) => {
                            if (res.data.code === 200) {
                                this.$message.success("删除成功");
                                this.getList();
                            } else {
                                this.$message.error(res.data.message);
                            }
                        });
                    })
                    .catch(() => {
                        this.$message.info("已取消删除");
                    });
            },
            handleViewDetails(schedule) {
                this.$router.push({
                    name: "scheduleDetail",
                    params: { scheduleId: schedule.id }
                });
                localStorage.setItem("scheduleId", schedule.id);
            },
            getList() {
                getScheduleList({ pageNum: 1, pageSize: 10 }).then((res) => {
                    this.schedules = res.data.data || [];
                });
            }
        },
        mounted() {
            this.getList();
        }
    };
</script>

<style lang="less" scoped>
    .schedule-schedule-management {
        height: 90%;
        .common-table {
            position: relative;
            height: calc(100% - 50px);
            /deep/ .el-table td,
            /deep/ .el-table th {
                text-align: center;
            }
        }
    }
</style>